#include "../lv_project_inc/lvgl_led.h"

//led
void lvgl_layout_led(lv_obj_t *obj)
{
    lv_obj_set_size(obj,200,100);
    lv_obj_align(obj,LV_ALIGN_BOTTOM_RIGHT,-225,-40);
    lv_obj_set_style_pad_all(obj,10,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);
    lv_obj_set_scrollbar_mode(obj,LV_SCROLLBAR_MODE_OFF);
    lv_obj_clear_flag(obj,LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_event_cb(obj,lvgl_led_event_handler,LV_EVENT_CLICKED,NULL);

    lv_img_t *img_sw = lv_img_create(obj);
    LV_IMG_DECLARE(sw_led);
    lv_img_set_src(img_sw,&sw_led);
    lv_obj_align(img_sw,LV_ALIGN_CENTER,0,20);
    lv_img_set_size_mode(img_sw,LV_IMG_SIZE_MODE_VIRTUAL);
    lv_img_set_zoom(img_sw,192);

    lvgl_led_init();

    led_lroom_s = lv_led_create(obj);
    lv_obj_set_size(led_lroom_s,30,30);
    lv_led_set_color(led_lroom_s,lv_color_hex(0xFFE4C4));
    lv_obj_set_style_bg_color(led_lroom_s,lv_color_hex(0xFFE4C4),0);
    lv_obj_align(led_lroom_s,LV_ALIGN_TOP_LEFT,0,0);

    led_mroom_s = lv_led_create(obj);
    lv_obj_set_size(led_mroom_s,30,30);
    lv_led_set_color(led_mroom_s,lv_color_hex(0xE6E6FA));
    lv_obj_set_style_bg_color(led_mroom_s,lv_color_hex(0xE6E6FA),0);
    lv_obj_align(led_mroom_s,LV_ALIGN_TOP_MID,-25,0);

    led_sroom_s = lv_led_create(obj);
    lv_obj_set_size(led_sroom_s,30,30);
    lv_led_set_color(led_sroom_s,lv_color_hex(0x9AFF9A));
    lv_obj_set_style_bg_color(led_sroom_s,lv_color_hex(0x9AFF9A),0);
    lv_obj_align(led_sroom_s,LV_ALIGN_TOP_MID,25,0);

    led_broom_s = lv_led_create(obj);
    lv_obj_set_size(led_broom_s,30,30);
    lv_led_set_color(led_broom_s,lv_color_hex(0xFFAEB9));
    lv_obj_set_style_bg_color(led_broom_s,lv_color_hex(0xFFAEB9),0);
    lv_obj_align(led_broom_s,LV_ALIGN_TOP_RIGHT,0,0);

    lvgl_led_controll();
}

//初始化LED
void lvgl_led_init()
{
    state_led_lroom = true;
    state_led_mroom = true;
    state_led_sroom = true;
    state_led_broom = true;

    fd_led = open("/dev/led_drv",O_RDWR);
    if(fd_led == -1)
    {
        perror("led open error");
        exit(0);
    }

    char buf[2] = {0};

    buf[0] = 1;
    buf[1] = 7;
    write(fd_led,buf,sizeof(buf));
    buf[1] = 8;
    write(fd_led,buf,sizeof(buf));
    buf[1] = 9;
    write(fd_led,buf,sizeof(buf));
    buf[1] = 10;
    write(fd_led,buf,sizeof(buf));
}

//LED事件处理
void lvgl_led_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
    {
        lv_obj_clear_flag(layout_led_controll,LV_OBJ_FLAG_HIDDEN);
    }
}

//LED控制面板事件处理
void lvgl_led_controll_event_handler(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)
    {
        if(obj == led_lroom_b)
        {
            printf("led_lroom\n");
            if(state_led_lroom == true)
            {
                printf("led_lroom off\n");
                lv_led_off(led_lroom_b);
                lv_obj_set_style_bg_opa(led_lroom_b,50,0);
                lv_led_off(led_lroom_s);
                lv_obj_set_style_bg_opa(led_lroom_s,50,0);
                
                char buf[2] = {0};

                buf[0] = 0;
                buf[1] = 7;
                write(fd_led,buf,sizeof(buf));

                state_led_lroom = false;
            }
            else
            {
                printf("led_lroom on\n");
                lv_led_on(led_lroom_b);
                lv_obj_set_style_bg_opa(led_lroom_b,LV_OPA_100,0);
                lv_led_on(led_lroom_s);
                lv_obj_set_style_bg_opa(led_lroom_s,LV_OPA_100,0);

                char buf[2] = {0};

                buf[0] = 1;
                buf[1] = 7;
                write(fd_led,buf,sizeof(buf));

                state_led_lroom = true;
            }
        }
        else if(obj == led_mroom_b)
        {
            printf("led_mroom\n");
            if(state_led_mroom == true)
            {
                printf("led_mroom off\n");
                lv_led_off(led_mroom_b);
                lv_obj_set_style_bg_opa(led_mroom_b,50,0);
                lv_led_off(led_mroom_s);
                lv_obj_set_style_bg_opa(led_mroom_s,50,0);
                
                char buf[2] = {0};

                buf[0] = 0;
                buf[1] = 8;
                write(fd_led,buf,sizeof(buf));

                state_led_mroom = false;
            }
            else
            {
                printf("led_mroom on\n");
                lv_led_on(led_mroom_b);
                lv_obj_set_style_bg_opa(led_mroom_b,LV_OPA_100,0);
                lv_led_on(led_mroom_s);
                lv_obj_set_style_bg_opa(led_mroom_s,LV_OPA_100,0);
                
                char buf[2] = {0};

                buf[0] = 1;
                buf[1] = 8;
                write(fd_led,buf,sizeof(buf));

                state_led_mroom = true;
            }
        }
        else if(obj == led_sroom_b)
        {
            printf("led_sroom\n");
            if(state_led_sroom == true)
            {
                printf("led_sroom off\n");
                lv_led_off(led_sroom_b);
                lv_obj_set_style_bg_opa(led_sroom_b,50,0);
                lv_led_off(led_sroom_s);
                lv_obj_set_style_bg_opa(led_sroom_s,50,0);
                
                char buf[2] = {0};

                buf[0] = 0;
                buf[1] = 9;
                write(fd_led,buf,sizeof(buf));

                state_led_sroom = false;
            }
            else
            {
                printf("led_sroom on\n");
                lv_led_on(led_sroom_b);
                lv_obj_set_style_bg_opa(led_sroom_b,LV_OPA_100,0);
                lv_led_on(led_sroom_s);
                lv_obj_set_style_bg_opa(led_sroom_s,LV_OPA_100,0);
                
                char buf[2] = {0};

                buf[0] = 1;
                buf[1] = 9;
                write(fd_led,buf,sizeof(buf));

                state_led_sroom = true;
            }
        }
        else
        {
            printf("led_broom\n");
            if(state_led_broom == true)
            {
                printf("led_broom off\n");
                lv_led_off(led_broom_b);
                lv_obj_set_style_bg_opa(led_broom_b,50,0);
                lv_led_off(led_broom_s);
                lv_obj_set_style_bg_opa(led_broom_s,50,0);

                char buf[2] = {0};

                buf[0] = 0;
                buf[1] = 10;
                write(fd_led,buf,sizeof(buf));

                state_led_broom = false;
            }
            else
            {
                printf("led_broom on\n");
                lv_led_on(led_broom_b);
                lv_obj_set_style_bg_opa(led_broom_b,LV_OPA_100,0);
                lv_led_on(led_broom_s);
                lv_obj_set_style_bg_opa(led_broom_s,LV_OPA_100,0);
                
                char buf[2] = {0};

                buf[0] = 1;
                buf[1] = 10;
                write(fd_led,buf,sizeof(buf));

                state_led_broom = true;
            }
        }
    }
}

//LED关闭控制面板处理
void lvgl_led_close_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);
    obj = lv_obj_get_parent(obj);
    
    if(code == LV_EVENT_CLICKED)
    {
        lv_obj_add_flag(layout_led_controll,LV_OBJ_FLAG_HIDDEN);
    }
}

//LED控制面板
void lvgl_led_controll()
{
    layout_led_controll = lv_obj_create(lv_scr_act());
    lv_obj_set_size(layout_led_controll,LV_HOR_RES,LV_VER_RES);
    lv_obj_center(layout_led_controll);

    lv_obj_t *layout_close = lv_obj_create(layout_led_controll);
    lv_obj_set_style_border_width(layout_close,0,0);
    lv_obj_set_scrollbar_mode(layout_close,LV_SCROLLBAR_MODE_OFF);
    lv_obj_clear_flag(layout_close,LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_align(layout_close,LV_ALIGN_TOP_RIGHT,0,0);
    lv_obj_set_size(layout_close,50,50);
    lv_obj_add_event_cb(layout_close,lvgl_led_close_event_handler,LV_EVENT_CLICKED,NULL);

    lv_label_t *label_close = lv_label_create(layout_close);
    lv_obj_center(label_close);
    lv_label_set_text(label_close,LV_SYMBOL_CLOSE);
    lv_obj_set_style_text_font(label_close,&lv_font_montserrat_32,0);

    lv_label_t *label_led_controll = lv_label_create(layout_led_controll);
    lv_obj_align(label_led_controll,LV_ALIGN_TOP_MID,0,0);
    lv_label_set_text(label_led_controll,"LED controller");
    lv_obj_set_style_text_font(label_led_controll,&lv_font_montserrat_28,0);

    led_lroom_b = lv_led_create(layout_led_controll);
    lv_obj_set_size(led_lroom_b,150,150);
    lv_led_set_color(led_lroom_b,lv_color_hex(0xFFE4C4));
    lv_obj_set_style_bg_color(led_lroom_b,lv_color_hex(0xFFE4C4),0);
    lv_obj_align(led_lroom_b,LV_ALIGN_LEFT_MID,0,0);
    lv_obj_add_event_cb(led_lroom_b,lvgl_led_controll_event_handler,LV_EVENT_CLICKED,NULL);
    if(state_led_lroom)
    {
        lv_led_on(led_lroom_b);
        lv_obj_set_style_bg_opa(led_lroom_b,LV_OPA_100,0);
    }
    else
    {
        lv_led_off(led_lroom_b);
        lv_obj_set_style_bg_opa(led_lroom_b,50,0);
    }

    label_led_controll = lv_label_create(layout_led_controll);
    lv_label_set_text(label_led_controll,"living room");
    lv_obj_align_to(label_led_controll,led_lroom_b,LV_ALIGN_OUT_BOTTOM_MID,-15,25);
    lv_obj_set_style_text_font(label_led_controll,&lv_font_montserrat_20,0);

    led_mroom_b = lv_led_create(layout_led_controll);
    lv_obj_set_size(led_mroom_b,150,150);
    lv_led_set_color(led_mroom_b,lv_color_hex(0xE6E6FA));
    lv_obj_set_style_bg_color(led_mroom_b,lv_color_hex(0xE6E6FA),0);
    lv_obj_align(led_mroom_b,LV_ALIGN_CENTER,-100,0);
    lv_obj_add_event_cb(led_mroom_b,lvgl_led_controll_event_handler,LV_EVENT_CLICKED,NULL);
    if(state_led_mroom)
    {
        lv_led_on(led_mroom_b);
        lv_obj_set_style_bg_opa(led_mroom_b,LV_OPA_100,0);
    }
    else
    {
        lv_led_off(led_mroom_b);
        lv_obj_set_style_bg_opa(led_mroom_b,50,0);
    }

    label_led_controll = lv_label_create(layout_led_controll);
    lv_label_set_text(label_led_controll,"master room");
    lv_obj_align_to(label_led_controll,led_mroom_b,LV_ALIGN_OUT_BOTTOM_MID,-15,25);
    lv_obj_set_style_text_font(label_led_controll,&lv_font_montserrat_20,0);

    led_sroom_b = lv_led_create(layout_led_controll);
    lv_obj_set_size(led_sroom_b,150,150);
    lv_led_set_color(led_sroom_b,lv_color_hex(0x9AFF9A));
    lv_obj_set_style_bg_color(led_sroom_b,lv_color_hex(0x9AFF9A),0);
    lv_obj_align(led_sroom_b,LV_ALIGN_CENTER,100,0);
    lv_obj_add_event_cb(led_sroom_b,lvgl_led_controll_event_handler,LV_EVENT_CLICKED,NULL);
    if(state_led_sroom)
    {
        lv_led_on(led_sroom_b);
        lv_obj_set_style_bg_opa(led_sroom_b,LV_OPA_100,0);
    }
    else
    {
        lv_led_off(led_sroom_b);
        lv_obj_set_style_bg_opa(led_sroom_b,50,0);
    }

    label_led_controll = lv_label_create(layout_led_controll);
    lv_label_set_text(label_led_controll,"secondary room");
    lv_obj_align_to(label_led_controll,led_sroom_b,LV_ALIGN_OUT_BOTTOM_MID,-15,25);
    lv_obj_set_style_text_font(label_led_controll,&lv_font_montserrat_20,0);

    led_broom_b = lv_led_create(layout_led_controll);
    lv_obj_set_size(led_broom_b,150,150);
    lv_led_set_color(led_broom_b,lv_color_hex(0xFFAEB9));
    lv_obj_set_style_bg_color(led_broom_b,lv_color_hex(0xFFAEB9),0);
    lv_obj_align(led_broom_b,LV_ALIGN_RIGHT_MID,0,0);
    lv_obj_add_event_cb(led_broom_b,lvgl_led_controll_event_handler,LV_EVENT_CLICKED,NULL);
    if(state_led_broom)
    {
        lv_led_on(led_broom_b);
        lv_obj_set_style_bg_opa(led_broom_b,LV_OPA_100,0);
    }
    else
    {
        lv_led_off(led_broom_b);
        lv_obj_set_style_bg_opa(led_broom_b,50,0);
    }

    label_led_controll = lv_label_create(layout_led_controll);
    lv_label_set_text(label_led_controll,"bathroom");
    lv_obj_align_to(label_led_controll,led_broom_b,LV_ALIGN_OUT_BOTTOM_MID,-15,25);
    lv_obj_set_style_text_font(label_led_controll,&lv_font_montserrat_20,0);

    lv_obj_add_flag(layout_led_controll,LV_OBJ_FLAG_HIDDEN);
}

//处理LED控制
void lvgl_led_controll_handle(lv_led_t *led_b,lv_led_t *led_s,char id_led,bool *state_led,bool flag)
{
    if(flag == true)
    {
        lvgl_lock();
        lv_led_on(led_b);
        lvgl_unlock();

        lvgl_lock();
        lv_obj_set_style_bg_opa(led_b,LV_OPA_100,0);
        lvgl_unlock();

        lvgl_lock();
        lv_led_on(led_s);
        lvgl_unlock();
        
        lvgl_lock();
        lv_obj_set_style_bg_opa(led_s,LV_OPA_100,0);
        lvgl_unlock();

        char buf[2] = {0};

        buf[0] = 1;
        buf[1] = id_led;
        write(fd_led,buf,sizeof(buf));

        *state_led = flag;

        if(state_led == true)
            printf("true\n");
        else if(state_led == false)
            printf("false\n");

    }
    else if(flag == false)
    {
        lvgl_lock();
        lv_led_off(led_b);
        lvgl_unlock();

        lvgl_lock();
        lv_obj_set_style_bg_opa(led_b,50,0);
        lvgl_unlock();

        lvgl_lock();
        lv_led_off(led_s);
        lvgl_unlock();
        
        lvgl_lock();
        lv_obj_set_style_bg_opa(led_s,50,0);
        lvgl_unlock();

        char buf[2] = {0};

        buf[0] = 0;
        buf[1] = id_led;
        write(fd_led,buf,sizeof(buf));

        *state_led = flag;
    }
}